/*
 *
 *
Copyright (c) 1998-2001 Aristar, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. The end-user documentation included with the redistribution, if any,
must include the following acknowledgment:

"This product includes software developed by Aristar, Inc. (http://www.aristar.com/)."

Alternately, this acknowledgment may appear in the software itself, if and
wherever such third-party acknowledgments normally appear.

4. The names "Dialect" and "Aristar, Inc." must not be used to endorse or
promote products derived from this software without prior written permission.
For written permission, please contact info@aristar.com.

5. Products derived from this software may not be called "Dialect", nor may
"Dialect" appear in their name, without prior written permission of Aristar, Inc.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
ARISTAR, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This software consists of voluntary contributions made by many individuals
on behalf of Aristar, Inc.  For more information on Aristar, Inc.,
please see <http://www.aristar.com/>.

Portions of this software are based upon software freely available and
written by individuals not associated with Aristar, Inc.  Copyrights are
maintained in the relevant source code files.
 *
 *
 */

#ifndef __CINTERPRETER_H__
#define __CINTERPRETER_H__

#include "DConfigGlobals.h"
#include "CValue.h"

////////////////////////////////////////////////////////////////////////
// Possible return values for CInterpreter::GetReturnFlag()
#define RETURN_NOT			0	// Not currently in the process of returning
#define RETURN_FUNCTION		1	// Following a Dialect return statement
#define RETURN_EXCEPTION	2	// Unwinding stack because of an exception
#define RETURN_ABORT		3	// Program was aborted by debugger
#define RETURN_DEAD			4	// Windows app received WM_QUIT and who knows what memory has been freed by Windows


class CInterpreter {
public:
	CInterpreter(){};
	virtual ~CInterpreter(){};
	virtual bool InterpretFile(LPCTSTR filename) = 0;
	virtual bool InterpretText(LPCTSTR text, VALUE context, LPCTSTR moduleName) = 0;
	virtual bool Evaluate(LPCTSTR str, VALUE& result, VALUE context) = 0;
	virtual bool Execute(VALUE func, VALUE context, int argCount, VALUE args[], VALUE& result) = 0;
	virtual bool ExecFuncFromCore(VALUE& result, VALUE vFunc, int argCount, VALUE args[], VALUE* self) = 0;
	virtual bool EvaluateFile(LPCTSTR filename, VALUE& result, VALUE& context) = 0;
	virtual bool DebugInterpretText(LPCTSTR text, TString& exceptionText) = 0;
	virtual bool DebugEvaluate(LPCTSTR text, VALUE& result, TString& exceptionText) = 0;
	virtual bool CompileFile(TString fileIn, TString fileOut, TString& exception) = 0;

	virtual void Abort() = 0;	// Tell interp to quit executing and unwind stack...
	virtual int GetReturnFlag() const = 0;
	virtual void ClearException() = 0;
	virtual VALUE GetException() = 0;
	virtual void SetExceptionInstance(VALUE vClassInstance) = 0;
	virtual void SetException(TString exc, TString msg, TString module, int line, VALUE vOther) = 0;
	virtual void SyntaxError(LPCTSTR parmString, ...) = 0;
	virtual void RuntimeError(LPCTSTR parmString, ...) = 0;
	virtual void InternalError(LPCTSTR parmString, ...) = 0;

	virtual int CurrentLine() = 0;
	virtual TString CurrentModuleName() = 0;
	virtual TString CurrentModulePath() = 0;
	virtual void RegisterCoreModule(TString moduleName, VALUE (*initFunc)()) = 0;

	virtual long IncOpenWindows() = 0;
	virtual long DecOpenWindows() = 0;
	virtual long GetOpenWindows() const = 0;
	virtual int GetStackLevel() const = 0;
	virtual VALUE CallStack() = 0;
};

//////////////////////////////////////////////////////
// Use this in core functions before trying to dereference the self pointer
// Obviously, this doesn't have to be done for "static" member functions
#define ASSERT_SELF(moduleName)	\
	if (self == NULL) { \
		pInterp->SetException(_T("eruntime"), _T("Class member function call missing 'this' object"), \
			((LPCTSTR)moduleName), 0, AllocNil());	\
		return AllocNil();	\
	}

//static bool _ASSERT_SELF(VALUE* pSelf)
//{
//	return pSelf != NULL;	// This, we can put a breakpoint on...
//}
//#define ASSERT_SELF(moduleName)	\
//	if (!_ASSERT_SELF(self)) {	\
//		pInterp->SetException(_T("eruntime"), _T("Class member function call missing 'this' object"), \
//			((LPCTSTR)moduleName), 0, AllocNil());	\
//		return AllocNil();		\
//	}

/////////////////////////////////////////////
#endif	// __CINTERPRETER_H__
